from docxtpl import DocxTemplate, InlineImage
from docx.shared import Mm
import os, datetime
import sys


def __get_date():
    year = str(datetime.datetime.now().year)
    month = str(datetime.datetime.now().month)
    num_dict = {
        '0': '〇', '1': '一', '2': '二', '3': '三', '4': '四', '5': '五', '6': '六',
        '7': '七', '8': '八', '9': '九', '10': '十', '11': '十一', '12': '十二'
    }
    new_year, new_month = '', num_dict[month]
    for i in range(len(year)):
        new_year += num_dict[year[i]]
    return '%s年%s月' % (new_year, new_month)


def run_word(project, sample, writer, verifier, outdir, volume_stat,
             distribution_cumulative_percent_images,
             distribution_interval_percent_images,
             distribution_interval_count_images,
             volume_image):
    doc = DocxTemplate('%s/docs/ontqc_template.docx' % sys.prefix)
    context = {
        'meta': {'writer': writer, 'verifier': verifier, 'date': __get_date(), 'project': project, 'sample': sample},
        'stat': volume_stat,
        'images': [
            {'image': InlineImage(doc, volume_image, width=Mm(160), height=Mm(120)), 'desc': '图5. 数据量分布图'},
        ]
    }
    count = 1
    for image in distribution_cumulative_percent_images:
        context['images'].append({
            'image': InlineImage(doc, image, width=Mm(160), height=Mm(120)),
            'desc': '图6.%d. 测序读长及碱基数累积分布图(百分比）' % count
        })
        count += 1
    count = 1
    for image in distribution_interval_percent_images:
        context['images'].append({
            'image': InlineImage(doc, image, width=Mm(160), height=Mm(120)),
            'desc': '图7.%d. 测序读长分布图(百分比）' % count
        })
        count += 1
    count = 1
    for image in distribution_interval_count_images:
        context['images'].append({
            'image': InlineImage(doc, image, width=Mm(160), height=Mm(120)),
            'desc': '图8.%d. 测序读长分布图(数量）' % count
        })
        count += 1
    doc.render(context)
    doc.save(os.path.join(outdir, "%s.ONT_Data_Report.docx" % project))
